package com.ruixin.config;

import com.ruixin.base.BaseTryCatch;
import com.ruixin.base.exception.BaseException;
import com.ruixin.base.factory.Factory;
import com.ruixin.base.util.BeanUtil;
import com.ruixin.base.util.StringUtil;

import java.util.*;

/**
 * Created by Ruixin on 2020/5/16.
 * Description:环境
 */
public class Environment {

    //配置文件
    private String configFile;

    //配置
    private Factory<String,String> paraFactory = new Factory<>();

    private Properties properties;

    public Environment(String configFile) throws BaseException {
        this.configFile = configFile;
        this.init();
    }

    public Environment(String configFile,String packages) throws BaseException {
        this.configFile = configFile;
        this.paraFactory.put(BeanConstant.KEY_PACKAGES,packages);
        this.init();
    }

    /**
     * 初始化操作
     * @throws BaseException
     */
    public void init() throws BaseException {
        BaseTryCatch.process(()->{
            //设置默认加载的包
            this.setProperty(BeanConstant.KEY_PACKAGES,BeanConstant.DEFAULT_PACKAGE);
            properties = BeanUtil.getProperties(this.getConfigFile());
            properties.stringPropertyNames().forEach((key)->{
                String property = properties.getProperty(key);
                this.setProperty(key,property);
            });
        });
    }

    /**
     * 获取配置文件的值
     * @param key
     * @return
     */
    public String getProperty(String key){
        return this.paraFactory.get(key);
    }

    /**
     * 获取配置文件的值
     * @param key
     * @param defaultValue
     * @return
     */
    public String getProperty(String key,String defaultValue){
        String property = this.paraFactory.getOrDefault(key,defaultValue);
        return property;
    }

    /**
     * 设置属性
     * @param key
     * @param value
     */
    public void setProperty(String key,String value){
        if(BeanConstant.KEY_PACKAGES.equals(key)){
            String beanPackage = this.paraFactory.get(key);
            if(StringUtil.isBlackOrNull(value)){
                return;
            }
            if(!StringUtil.isBlackOrNull(beanPackage)){
                value = beanPackage+","+value;
            }
        }
        this.paraFactory.put(key,value);
    }

    public boolean containsKey(String key){
        return this.paraFactory.containsKey(key);
    }

    public String getBeanPackage(){
        return this.paraFactory.get(BeanConstant.KEY_PACKAGES);
    }

    public void setParaFactory(Factory<String, String> paraFactory) {
        this.paraFactory = paraFactory;
    }

    public Factory<String, String> getParaFactory() {
        return paraFactory;
    }

    public String getConfigFile() {
        if(StringUtil.isBlackOrNull(configFile)){
            this.configFile =  BeanConstant.DEFAULT_CONFIGFILE;
        }
        return configFile;
    }

    public void setConfigFile(String configFile) {
        this.configFile = configFile;
    }

    public Properties getProperties() {
        return properties;
    }
}
